﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecommerce.Model;

namespace Ecommerce.Web.Admin.Controles
{
    public partial class cup_PesquisaRevenda : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                this.CarregarRevenda();
        }

        private void CarregarRevenda()
        {
            var revendas = RevendaModel.Instance.Listar();
            grvRevenda.DataSource = revendas;
            grvRevenda.DataBind();
        }

        protected void grvRevenda_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var revenda = (tb_rev_revenda)e.Row.DataItem;
                e.Row.Cells[0].Text = revenda.rev_idt_revenda.ToString();
                e.Row.Cells[1].Text = revenda.tb_pju_pessoa_juridica.pju_des_razao_social;
                e.Row.Cells[2].Text = revenda.tb_pju_pessoa_juridica.pju_num_cnpj;
                e.Row.Cells[3].Text = revenda.tb_pju_pessoa_juridica.pju_nom_contato;
                e.Row.Cells[4].Text = revenda.rev_cod_revenda;
                e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackClientHyperlink(this.grvRevenda, "Select$" + e.Row.RowIndex));
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            if (((bool)Session["CupomSalvo"]) == true)
            {
                GridViewRow row = grvRevenda.SelectedRow;
                int identificadorRevenda = Convert.ToInt32(row.Cells[0].Text);

                //Obtenho o cupom
                var cupom = CupomModel.Instance.Obter(Convert.ToInt32(Session["IdentificadorCupom"]));
                cupom.rev_idt_revenda = Convert.ToInt32(identificadorRevenda);
                CupomModel.Instance.Inserir(cupom);
            }
        }
    }
}